//
//  ball.m
//  baseball
//
//  Created by Donggi Hahm on 10. 03. 14.
//  Copyright 2010 ComTalk. All rights reserved.
//

#import "ball.h"
#import "global.h"

@implementation ball

@synthesize spBall, spBallShadow, nCurrentFrame, nTotalFrame;

- (id) init
{
	m_windowSize = [[CCDirector sharedDirector] winSize];
	
	if ( m_windowSize.width == 768 )
		m_DeviceKind = DEVICE_IPAD;
	else
		m_DeviceKind = DEVICE_IPHONE;
	
	[self GenerateBatteryViewBallPath];
	nCurrentFrame = 0;
	[spBall setPosition: ccp(nX[0],nY[0])];
	[spBall setScale:0.1f];
	[spBallShadow setPosition: ccp(nX[0],nShadowY[0])];
	[spBallShadow setScale:0.1f];
	
	return self;
}

- (void) GenerateBatteryViewBallPath
{
	int nIndex, startX, startY, startShadowY, locationX, locationY, locationShadowY, ballX, ballY, ballShadowY, ballTotalFrame, doubleTotalFrame, ratio;
	int nLevelH = 0, nLevelV = 0;
	float nTmpSize;
	
	startX = BALL_START_X;
	startY = BALL_START_Y;
	startShadowY = BALL_SHADOW_START_Y;
	locationX = startX+60;
	locationY = startY-300;
	locationShadowY = locationY-100;
	nTmpSize = 0.1f;
	
	//ball speed
	if ( gameInfo.nBallCnt < 10 ) 
		ballTotalFrame = 21;
	else if ( gameInfo.nBallCnt < 20 ) 
		ballTotalFrame = 20;
	else 
		ballTotalFrame = 19;
	
	//ball type
	int nBallKind[4];
	int nBallKindCnt = 0;
	
	if ( gameInfo.blnPracticeMode == FALSE )
	{
		gameInfo.blnFastball = TRUE;
		gameInfo.blnCurveball = TRUE;
		gameInfo.blnSlider = TRUE;
		gameInfo.blnChangeup = TRUE;
	}
	
	if ( gameInfo.blnFastball )
	{
		nBallKind[nBallKindCnt] = BALLKIND_4SEAM;
		nBallKindCnt++;
	}
	if ( gameInfo.blnCurveball )
	{
		nBallKind[nBallKindCnt] = BALLKIND_CURVE;
		nBallKindCnt++;
	}
	if ( gameInfo.blnSlider )
	{
		nBallKind[nBallKindCnt] = BALLKIND_SLIDER;
		nBallKindCnt++;
	}
	if ( gameInfo.blnChangeup )
	{
		nBallKind[nBallKindCnt] = BALLKIND_CHANGEUP;
		nBallKindCnt++;
	}
	
	if ( gameInfo.nBallCnt < 5 )
		nBallType = nBallKind[0];
	else
		nBallType = nBallKind[ rand()%nBallKindCnt ];
	
	switch ( nBallType )
	{
		case BALLKIND_4SEAM :
			nLevelH = 0;
			nLevelV = 0;
			ballTotalFrame--;
			break;
		case BALLKIND_CURVE :
			nLevelH = 30;
			nLevelV = -300;
			ballTotalFrame++;
			break;
		case BALLKIND_SLIDER :
			nLevelH = 200;
			nLevelV = 30;
			ballTotalFrame++;
			break;
		case BALLKIND_CHANGEUP :
			nLevelH = 0;
			nLevelV = 100;
			ballTotalFrame+=5;
			break;
	}
	
	doubleTotalFrame = ballTotalFrame * ballTotalFrame;
	for ( nIndex = 0 ; nIndex < ballTotalFrame + EXTRA_FRAME ; nIndex++ )
	{
		ratio = nIndex * nIndex;
		
		ballX = 100 * ( startX * ( doubleTotalFrame - ratio ) + locationX * ratio ) / doubleTotalFrame;
		ballY = 100 * ( startY * ( doubleTotalFrame - ratio ) + locationY * ratio ) / doubleTotalFrame;
		ballShadowY = 100 * ( startShadowY * ( doubleTotalFrame - ratio ) + locationShadowY * ratio ) / doubleTotalFrame;
		
		if ( nBallType == BALLKIND_SLIDER || nBallType == BALLKIND_CURVE || nBallType == BALLKIND_CHANGEUP )	//slider
		{
			ballX -= nLevelH * PATH_SLIDER[ 50 * nIndex / ballTotalFrame ] * ( nIndex + 5 ) / ( ballTotalFrame + 5 ) / 10;
			ballY -= nLevelV * PATH_SLIDER[ 50 * nIndex / ballTotalFrame ] * ( nIndex + 5 ) / ( ballTotalFrame + 5 ) / 10;
		}
		else if ( nBallType == BALLKIND_SINKER )		//sinker
		{
			ballX -= nLevelH * PATH_SINKER[ 50 * nIndex / ballTotalFrame ] * ( nIndex + 5 ) / ( ballTotalFrame + 5 ) / 5;
			ballY -= nLevelV * PATH_SINKER[ 50 * nIndex / ballTotalFrame ] * ( nIndex + 5 ) / ( ballTotalFrame + 5 ) / 5;
		}
		
		nX[ nIndex ] = ballX / 100;
		nY[ nIndex ] = ballY / 100;
		nShadowY[ nIndex ] = ballShadowY / 100;
		
		nTmpSize += 0.05f;
		nSize[ nIndex ] = nTmpSize;		
	}
	nTotalFrame = ballTotalFrame;
}

- (void) GenerateBatteryViewBattingBall
{
	int nFinishShadowY = 0;
	
	hitInfo.hittingDistance = 100 + rand()%800;
	hitInfo.hittingAngleH = rand()%(FOUL_BALL_ANGLE*2) - FOUL_BALL_ANGLE;

	if ( hitInfo.blnHitTimingGood )
		hitInfo.hittingAngleV = 40 + rand()%30;
	else
		hitInfo.hittingAngleV = rand()%20;
	
	int nStartX = nX[nCurrentFrame-1];
	int nStartY = nY[nCurrentFrame-1];
	int nStartShadowY = nShadowY[nCurrentFrame-1];

	int nDirectionX = hitInfo.hittingAngleH * hitInfo.hittingAngleH * 100 / FOUL_BALL_ANGLE / FOUL_BALL_ANGLE;
	if ( hitInfo.hittingAngleH < 0 ) nDirectionX *= -1;

	int nDirectionY = 20;
	
	if ( m_DeviceKind == DEVICE_IPAD )
	{
		hitInfo.hittingAngleV *= 1.5;
	}
	
	int nFinishX = nStartX - nDirectionX;
	int nFinishY = (nStartY + nDirectionY) + (hitInfo.hittingAngleV * hitInfo.hittingAngleV / 20);
	
	if ( hitInfo.blnHitTimingGood )
		nFinishShadowY = nStartShadowY + nDirectionY;
	else
		nFinishShadowY = nFinishY;
	
	int nMaxFrame = MAX_FRAME_HITTING_BALL - (hitInfo.hittingDistance/200);
	float nTmpSize = nSize[nCurrentFrame-1];
	for ( int nIndex = 0 ; nIndex < nMaxFrame+EXTRA_FRAME ; nIndex++ )
	{		
		nX[ nIndex ] = ( nStartX * ( nMaxFrame - nIndex ) + nFinishX * nIndex ) / nMaxFrame;
		nY[ nIndex ] = ( nStartY * ( nMaxFrame - nIndex ) + nFinishY * nIndex ) / nMaxFrame;
		nShadowY[ nIndex ] = ( nStartShadowY * ( nMaxFrame - nIndex ) + nFinishShadowY * nIndex ) / nMaxFrame;
		
		nTmpSize -= 0.05f;
		nSize[ nIndex ] = nTmpSize;
	}
	
	nCurrentFrame = 0;
}

- (int) GetX: (int)nFrameIdx
{
	return nX[ nFrameIdx ];
}

- (int) GetY: (int)nFrameIdx
{
	return nY[ nFrameIdx ];
}

- (int) GetShadowY: (int)nFrameIdx
{
	return nShadowY[ nFrameIdx ];
}

- (float) GetSize: (int)nFrameIdx
{	
	return nSize[ nFrameIdx ];
}

@end



















